package com.primb.bench.project.home.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.flyco.dialog.widget.NormalDialog;
import com.primb.baselibs.view.loading.SpotsDialog;
import com.primb.bench.R;
import com.primb.bench.base.BaseFragment;
import com.primb.bench.common.constant.AppConstant;
import com.primb.bench.common.utils.RoleLevelConvert;
import com.primb.bench.project.home.activity.LoanCalculateActivity;
import com.primb.bench.project.home.activity.LoanStockActivity;
import com.primb.bench.project.home.contract.HomeContract;
import com.primb.bench.project.home.presenter.HomePresenterImpl;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * Created by Chen on 2017/4/17.
 * 功能描述：
 */

public class HomeFragment extends BaseFragment implements HomeContract.View {
    @BindView(R.id.tv_home_title)
    TextView tvHomeTitle;
    private HomeContract.Presenter mPresenter;
    private SpotsDialog dialog;

    @Override
    protected int onBaseLayoutId() {
        return R.layout.fragment_home;
    }

    @Override
    protected void onBaseBindView() {
        int statusBarHeight = -1;
        //获取status_bar_height资源的ID
        int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
        if (resourceId > 0) {
            //根据资源ID获取响应的尺寸值
            statusBarHeight = getResources().getDimensionPixelSize(resourceId);
            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
            layoutParams.setMargins(0, statusBarHeight + 12, 0, 15);
//            tvApprovalTitle.setTextSize(18);
            tvHomeTitle.setLayoutParams(layoutParams);
        }
    }

    @Override
    protected void onBaseLoadData() {
        mPresenter = new HomePresenterImpl(this);
        mPresenter.start();
    }

    @Override
    public void showErrorMsg(String msg) {
        mActivity.showToast(msg);
    }

    @Override
    public void showLoading() {
        dialog = new SpotsDialog(getContext(), "加载中");
        dialog.show();
    }

    @Override
    public void hideLoading() {
        if (dialog != null && dialog.isShowing()) {
            dialog.dismiss();
        }
    }

    @Override
    public void refreshSpot(String num) {

    }


    @Override
    public void onPause() {
        super.onPause();
        mPresenter.onRecycle();
    }

    @OnClick({R.id.tv_home_add_price, R.id.tv_home_add_measure,
            R.id.tv_home_stock_measure, R.id.tv_home_stock_price,
            R.id.tv_home_same_measure})
    public void onViewClicked(View view) {

        Intent intent = new Intent();
        switch (view.getId()) {
            case R.id.tv_home_add_measure:
                if (AppConstant.ROLE_LEVEL_ENUM == RoleLevelConvert.RoleLevel.MANAGER) {
                    intent.setClass(getActivity(), LoanCalculateActivity.class);
                    intent.putExtra("type", AppConstant.LOAN_PRICE);
                    startActivity(intent);
                } else {
                    showAlterDialogOne("提示", "抱歉，此功能不在您的权限范围");
                }
                break;
            case R.id.tv_home_stock_price:
                if (AppConstant.ROLE_LEVEL_ENUM == RoleLevelConvert.RoleLevel.MANAGER) {
                    intent.setClass(getActivity(), LoanStockActivity.class);
                    intent.putExtra("type", AppConstant.LOAN_PRICE);
                    startActivity(intent);
                } else {
                    showAlterDialogOne("提示", "抱歉，此功能不在您的权限范围");
                }
                break;
            case R.id.tv_home_stock_measure:
                intent.setClass(getActivity(), LoanStockActivity.class);
                intent.putExtra("type", AppConstant.LOAN_CALCULATE);
                startActivity(intent);
                break;
            case R.id.tv_home_add_price:
                intent.setClass(getActivity(), LoanCalculateActivity.class);
                intent.putExtra("type", AppConstant.LOAN_CALCULATE);
                startActivity(intent);
                break;
            case R.id.tv_home_same_measure:
                intent.setClass(getActivity(), LoanCalculateActivity.class);
                startActivity(intent);
               break;
        }
    }

}
